package com.shoparc.maidenlane;

import java.util.ArrayList;

import org.apache.commons.math3.geometry.euclidean.threed.Rotation;
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import com.shoparc.geo.Element;
import com.shoparc.geo.Quad;

public class PanelBrick extends Panel {
	Element glassPlane;
	Rotation planeRot;
	ArrayList<Quad>frame;

	PanelBrick(Vector3D p1, Vector3D p2, Vector3D p3, Vector3D p4) {
		super(p1, p2, p3, p4);
		ArrayList<Vector3D> gpArr = new ArrayList<Vector3D>();
		gpArr.add(new Vector3D(p1.getX(), p1.getY(), p1.getZ()));
		gpArr.add(new Vector3D(p2.getX(), p2.getY(), p2.getZ()));
		gpArr.add(new Vector3D(p3.getX(), p3.getY(), p3.getZ()));
		gpArr.add(new Vector3D(p4.getX(), p4.getY(), p4.getZ()));
		glassPlane = new Element(gpArr);
		
		

	}
	
	PanelBrick(ArrayList<Vector3D> vertices) {
		super(vertices);
		ArrayList<Vector3D> gpArr = new ArrayList<Vector3D>();
		for(Vector3D v : vertices){
			gpArr.add(new Vector3D(v.getX(), v.getY(), v.getZ()));
		}
		
		glassPlane = new Element(vertices);
	
	}
	
	public void translate(Vector3D t){
		super.translate(t);
		glassPlane.translate(t);
	}
	
	public void rotate(Rotation r){
		super.rotate(r);
		
		//TODO this is currently broken
		for(int i =0; i<vertices.size(); i++){
			Vector3D v = vertices.get(i);
			v = v.subtract(barycenter);
			v = r.applyTo(v);
			v = v.add(barycenter);
			vertices.set(i, v);
		}
	}
	
	public void scale(double x, double y, double z){
		super.scale(x,  y,  z);
		glassPlane.scale(x,  y,  z);
	}
	
	public void buildFrame(){
		frame = new ArrayList<Quad>();
		Vector3D p00, p01, p02, p03, p10, p11, p12, p13, p20, p21, p22, p23, p30, p31, p32, p33;
		p00 	 =  vertices.get(0);
		p01 	 =  vertices.get(1);
		p02 	 =  vertices.get(2);
		p03 	 =  vertices.get(3);
		
		p10 	 =  glassPlane.vertices.get(0);
		p11 	 =  glassPlane.vertices.get(1);
		p12 	 =  glassPlane.vertices.get(2);
		p13 	 =  glassPlane.vertices.get(3);
		
		p20 =  new Vector3D(p10.getX(), p10.getY(), p10.getZ());
		p21 =  new Vector3D(p11.getX(), p11.getY(), p11.getZ());
		p22 =  new Vector3D(p12.getX(), p12.getY(), p12.getZ());
		p23 =  new Vector3D(p13.getX(), p13.getY(), p13.getZ());
		
		p30 =  new Vector3D(p00.getX(), p00.getY(), p00.getZ());
		p31 =  new Vector3D(p01.getX(), p01.getY(), p01.getZ());
		p32 =  new Vector3D(p02.getX(), p02.getY(), p02.getZ());
		p33 =  new Vector3D(p03.getX(), p03.getY(), p03.getZ());
		
		Vector3D n1 = getNormal();
		n1 = n1.scalarMultiply(-3.0);
		//p00 =  p00.subtract(n1);
		//p01 =  p01.subtract(n1);
		//p02 =  p02.subtract(n1);
		//p03 =  p03.subtract(n1);
		
		//p10 =  p10.subtract(n1);
		//p11 =  p11.subtract(n1);
		//p12 =  p12.subtract(n1);
		//p13 =  p13.subtract(n1);
		
		
		
		p20 =  p20.add(n1);
		p21 =  p21.add(n1);
		p22 =  p22.add(n1);
		p23 =  p23.add(n1);
		
		p30 =  p30.add(n1);
		p31 =  p31.add(n1);
		p32 =  p32.add(n1);
		p33 =  p33.add(n1);
		
		Quad q00 = new Quad(p00, p10, p11, p01);
		Quad q01 = new Quad(p01, p11, p12, p02);
		Quad q02 = new Quad(p02, p12, p13, p03);
		Quad q03 = new Quad(p03, p13, p10, p00);

		frame.add(q00);
		frame.add(q01);
		frame.add(q02);
		frame.add(q03);
		
		Quad q10 = new Quad(p20, p10, p11, p21);
		Quad q11 = new Quad(p21, p11, p12, p22);
		Quad q12 = new Quad(p22, p12, p13, p23);
		Quad q13 = new Quad(p23, p13, p10, p20);

		frame.add(q10);
		frame.add(q11);
		frame.add(q12);
		frame.add(q13);
		
		Quad q20 = new Quad(p20, p30, p31, p21);
		Quad q21 = new Quad(p21, p31, p32, p22);
		Quad q22 = new Quad(p22, p32, p33, p23);
		Quad q23 = new Quad(p23, p33, p30, p20);

		frame.add(q20);
		frame.add(q21);
		frame.add(q22);
		frame.add(q23);
		
		Quad q30 = new Quad(p30, p00, p01, p31);
		Quad q31 = new Quad(p31, p01, p02, p32);
		Quad q32 = new Quad(p32, p02, p03, p33);
		Quad q33 = new Quad(p33, p03, p00, p30);

		frame.add(q30);
		frame.add(q31);
		frame.add(q32);
		frame.add(q33);
	}
	
	
	public void buildFrameFlatFace(){
		frame = new ArrayList<Quad>();
		Vector3D p00, p01, p02, p03, p10, p11, p12, p13, p20, p21, p22, p23, p30, p31, p32, p33;
		p00 	 =  vertices.get(0);
		p01 	 =  vertices.get(1);
		p02 	 =  vertices.get(2);
		p03 	 =  vertices.get(3);
		
		p10 	 =  glassPlane.vertices.get(0);
		p11 	 =  glassPlane.vertices.get(1);
		p12 	 =  glassPlane.vertices.get(2);
		p13 	 =  glassPlane.vertices.get(3);
		
		p20 =  new Vector3D(p10.getX(), p10.getY(), p10.getZ());
		p21 =  new Vector3D(p11.getX(), p11.getY(), p11.getZ());
		p22 =  new Vector3D(p12.getX(), p12.getY(), p12.getZ());
		p23 =  new Vector3D(p13.getX(), p13.getY(), p13.getZ());
		
		p30 =  new Vector3D(p00.getX(), p00.getY(), p00.getZ());
		p31 =  new Vector3D(p01.getX(), p01.getY(), p01.getZ());
		p32 =  new Vector3D(p02.getX(), p02.getY(), p02.getZ());
		p33 =  new Vector3D(p03.getX(), p03.getY(), p03.getZ());
		
		Vector3D n1 = getNormal();
		//n1 = n1.scalarMultiply(1.5);
		
		//p00 =  p00.subtract(n1);
		//p01 =  p01.subtract(n1);
		//p02 =  p02.subtract(n1);
		//p03 =  p03.subtract(n1);
		
		//p10 =  p10.subtract(n1);
		//p11 =  p11.subtract(n1);
		//p12 =  p12.subtract(n1);
		//p13 =  p13.subtract(n1);
		
		n1  = getNormal();
		Vector3D n2 = n1.scalarMultiply(.75);

		
		p20 =  p20.add(n2);
		p21 =  p21.add(n2);
		p22 =  p22.add(n2);
		p23 =  p23.add(n2);
		
		p30 =  p30.add(n2);
		p31 =  p31.add(n2);
		p32 =  p32.add(n2);
		p33 =  p33.add(n2);
		glassPlane.translate(n2);
		
		
		Quad q00 = new Quad(p00, p10, p11, p01);
		Quad q01 = new Quad(p01, p11, p12, p02);
		Quad q02 = new Quad(p02, p12, p13, p03);
		Quad q03 = new Quad(p03, p13, p10, p00);

		frame.add(q00);
		frame.add(q01);
		frame.add(q02);
		frame.add(q03);
		
		Quad q10 = new Quad(p20, p10, p11, p21);
		Quad q11 = new Quad(p21, p11, p12, p22);
		Quad q12 = new Quad(p22, p12, p13, p23);
		Quad q13 = new Quad(p23, p13, p10, p20);

		frame.add(q10);
		frame.add(q11);
		frame.add(q12);
		frame.add(q13);
		
		Quad q20 = new Quad(p20, p30, p31, p21);
		Quad q21 = new Quad(p21, p31, p32, p22);
		Quad q22 = new Quad(p22, p32, p33, p23);
		Quad q23 = new Quad(p23, p33, p30, p20);

		frame.add(q20);
		frame.add(q21);
		frame.add(q22);
		frame.add(q23);
		
		Quad q30 = new Quad(p30, p00, p01, p31);
		Quad q31 = new Quad(p31, p01, p02, p32);
		Quad q32 = new Quad(p32, p02, p03, p33);
		Quad q33 = new Quad(p33, p03, p00, p30);

		frame.add(q30);
		frame.add(q31);
		frame.add(q32);
		frame.add(q33);
	}
	
	
	

}























